package com.jctrip.hotel.source.xxl.hmein.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.service.hmein.HmeinService;
import com.jctrip.hotel.source.service.hmein.HmeinTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author: zxy
 * @CreateTime: 2023-08-15 16:15
 * @Description:
 */
@Slf4j
public class SyncHmeinHotspotRatesTaskThread implements Runnable {

    private final String appId;
    private final String memberNo;
    private final Integer days;
    private final Map<String, String> rateCodeWithSourceIdMap;


    public SyncHmeinHotspotRatesTaskThread(String appId, String memberNo, Integer days, Map<String, String> rateCodeWithSourceIdMap) {
        this.appId = appId;
        this.memberNo = memberNo;
        this.days = days;
        this.rateCodeWithSourceIdMap = rateCodeWithSourceIdMap;
    }

    @Override
    public void run() {
        RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
        HmeinTaskService hmeinTaskService = BeanContext.getApplicationContext().getBean(HmeinTaskService.class);
        HmeinService hmeinService = BeanContext.getApplicationContext().getBean(HmeinService.class);
        while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_HMEIN_HOTSPOT_RATES_LIST) > 0) {
            try {
                String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_HMEIN_HOTSPOT_RATES_LIST);
                if (StringUtils.isBlank(hotelId)) {
                    continue;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                String start = simpleDateFormat.format(date);
                String end = simpleDateFormat.format(DateUtils.addDays(date, days));
                hmeinTaskService.syncHmeinRatesTaskPop(appId, hmeinService.parseHotelId(hotelId), memberNo, start, end, rateCodeWithSourceIdMap);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}